#include "threadpool.h"

void* thread_func(void* arg){
    threadPool_t* Ppool = (threadPool_t*)arg;
    /* pthread_mutex_lock(&Ppool->mutex); */
    /* MYSQL* mysql = mysql_init(NULL); */
    /* pthread_mutex_unlock(&Ppool->mutex); */
    while(1){
        pthread_mutex_lock(&Ppool->mutex);
        while(Ppool->taskQueue.queueSize == 0 && Ppool->exitFlag == 0){
            pthread_cond_wait(&Ppool->cond,&Ppool->mutex);
        }
        if(Ppool->exitFlag == 1){
            pthread_mutex_unlock(&Ppool->mutex);
            pthread_exit(NULL);
        }
        int netfd = Ppool->taskQueue.front->netfd;
        dequeue(&Ppool->taskQueue);
        pthread_mutex_unlock(&Ppool->mutex);
        while(1){
            deal_message(&netfd,Ppool);
            if(netfd == -1){
                break;
            }
        } 
    }
    return NULL;
}

int make_worker(threadPool_t* pool){
    for(int i = 0; i < pool->threadNum; i++){
        pthread_create(&pool->childThreadSet[i],NULL,thread_func,pool);
    }
    return 0;
};


